package com.tinywind.Sword.Controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tinywind.Sword.Entity.TodoEntity;
import com.tinywind.Sword.Entity.TodoFilter;
import com.tinywind.Sword.Service.TodoService;
import com.tinywind.Sword.Utility.GaJTimeManager;
import com.tinywind.Sword.Utility.JsJsonManager;
import com.google.gson.Gson;
import com.tinywind.Sword.Dto.Data;
import com.tinywind.Sword.Dto.Page;
import com.tinywind.Sword.Dto.Result;

@RestController
@CrossOrigin
@RequestMapping("/Todo") // url:/模块/资源/{id}/细分 /seckill/list
public class TodoController extends BaseController {
		private Logger logger = LoggerFactory.getLogger(this.getClass());
	
		@Autowired
		private TodoService service;
		
		@RequestMapping(value = "/Search", method = RequestMethod.POST)
		@ResponseBody
		public Result<TodoEntity> search(@RequestBody TodoFilter filter) {
			int userId = this.getUserId();
			filter.userId = userId;
			String info = String.format("Todo filter is; %s", JsJsonManager.getJson(filter));
			logger.info(info);
			try {
				List<TodoEntity> list = service.search(filter);
				int totalCount = service.getTotalCount(filter);
//				logger.debug("TodoController/search success, id is %d",id);
				Page pageInfo = new Page(filter.page,filter.pageSize,totalCount);
				Data<TodoEntity> data = new Data<TodoEntity>(list,pageInfo);
				Result<TodoEntity> result = new Result<TodoEntity>(0,data,list.size());
				return result;
			}
			catch (Exception e) {
				logger.debug("TodoController/search exception,%s",e.getMessage());
				e.printStackTrace();
				return new Result<TodoEntity>(-1);
			}
			
		}
	
		@RequestMapping(value = "/Delete", method = RequestMethod.POST)
		public Result<TodoEntity> delete(Long id) {
			int userId = this.getUserId();
			System.out.format("\n\n\n\nTodo id is %d,user is is: %d\n\n\n", id,userId);
			try {
				service.delete(id.intValue(),userId);
			} catch (Exception e) {
				System.out.format("\n\n\n\n delete error!!! \n\n\n");
				e.printStackTrace();
			}
			return new Result<TodoEntity>(0);
		}
	
		@RequestMapping(value = "/Insert", method = RequestMethod.POST)
		public Result<TodoEntity> insert(String title, String detail,Integer actionState,Boolean isDelete,Integer startState,Integer finishState,String planStartTime,Integer planStartTime_action,String planFinishTime,String actualStartTime,String actualFinishTime,Integer parentId,Integer type,String summary) {
			Integer userId = getUserId();
			TodoEntity entity = new TodoEntity();
			
			if(title != null) {
				entity.title = title;
			}
			if(detail != null) {
				entity.detail = detail;
			}
			
			if(actionState != null) {
				entity.actionState = actionState;
			}
			
			if(isDelete != null) {
				entity.isDelete = isDelete;
			}
			if(startState != null) {
				entity.startState = startState;
			}
			if(finishState != null) {
				entity.finishState = finishState;
			}
			if(planStartTime != null) {
				entity.planStartTime = planStartTime;
			}
			if(planFinishTime != null) {
				entity.planFinishTime = planFinishTime;
			}
			if(actualStartTime != null) {
				entity.actualStartTime = actualStartTime;
			}
			if(actualFinishTime != null) {
				entity.actualFinishTime = actualFinishTime;
			}
			if(parentId != null) {
				entity.parentId = parentId;
			}
			if(type != null) {
				entity.type = type;
			}
			//不需要判空
			entity.summary = summary;
			
			
			String now = GaJTimeManager.sharedInstance.stdTimeStringForNow();
			entity.createTime = now;
			entity.updateTime = now;
			try {
				// 返回值是一个bool值,应该是返回影响的条数.新增的id直接在entity中.
				service.insert(entity,userId);
				logger.debug("TodoController/Insert success, todoId is: %d,userId is: %d",entity.id,userId);
				Data<TodoEntity> data = new Data<TodoEntity>(entity);
				Result<TodoEntity> result = new Result<TodoEntity>(0,data);
				return result;
			} catch (Exception e) {
				logger.debug("TodoController/Insert exception,%s",e.getMessage());
				e.printStackTrace();
				return new Result<TodoEntity>(0);
			}
		}
		
		
		//null代表没有初始值,不应该更新为
		@RequestMapping(value = "/Update", method = RequestMethod.POST)
		public Result<TodoEntity> update(int id,String title, String detail,Integer actionState,Boolean isDelete,Integer startState,Integer finishState,String planStartTime,Integer planStartTime_action,String planFinishTime,String actualStartTime,String actualFinishTime,Integer parentId,Integer type,String summary) {
			TodoFilter filter = new TodoFilter();
//			0,1,id,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null
			List<TodoEntity> al = (List<TodoEntity>) service.search(filter);
			
			TodoEntity entity = null;
			//entity为null
			if(al == null || al.size()== 0) {
				return new Result<TodoEntity>(0,"Invalid id");
			}
			else {
				entity = al.get(0);
			}
			
			//先查找,然后有值的修改,没有值的不改.
			if(title != null) {
				entity.title = title;
			}
			if(detail != null) {
				entity.detail = detail;
			}
			
			if(actionState != null) {
				entity.actionState = actionState;
			}
			
			if(isDelete != null) {
				entity.isDelete = isDelete;
			}
			if(startState != null) {
				entity.startState = startState;
			}
			if(finishState != null) {
				entity.finishState = finishState;
			}
			if(planStartTime != null) {
				entity.planStartTime = planStartTime;
			}
			if(planFinishTime != null) {
				entity.planFinishTime = planFinishTime;
			}
			if(actualStartTime != null) {
				entity.actualStartTime = actualStartTime;
			}
			if(actualFinishTime != null) {
				entity.actualFinishTime = actualFinishTime;
			}
			if(parentId != null) {
				entity.parentId = parentId;
			}
			if(type != null) {
				entity.type = type;
			}
			if(summary != null) {
				entity.summary = summary;
			}
		
			String now = GaJTimeManager.sharedInstance.stdTimeStringForNow();
			entity.updateTime = now;
			
			try {
				// 返回值是一个bool值,应该是返回影响的条数.新增的id直接在entity中.
				service.update(entity);
				Data<TodoEntity> data = new Data<TodoEntity>(entity);
				Result<TodoEntity> result = new Result<TodoEntity>(0,data);
				return result;
			} catch (Exception e) {
				logger.debug("TodoController/Update exception,%s",e.getMessage());
				e.printStackTrace();
				return new Result<TodoEntity>(-1);
			}
		}
	}